﻿<cfscript>
/** 
* ORM ClassroomTimeTable
* 
* @hint "Hibernate persistenter t_clsroom_timetable. 教室时间占用情况表" 
* 
*/ 

component 
	displayname="public.service.orm.senate.ClassroomTimeTable" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="ClassroomTimeTable" 
	table="t_clsroom_timetable"
	{

	property type="string" name="ClassRoomTimeID" column="crtime_id" fieldtype="id" generator="assigned" hint="教室使用编号";

	property type="string" name="UseIndex" column="uidx" hint="使用序号";
	property type="string" name="WeekIndex" column="widx" hint="周次序列";

	property type="string" name="WeekDay" column="wdidx" hint="星期几";
	property type="numeric" name="CellStart" column="cellidx" hint="起点节次";
	property type="numeric" name="CellLength" column="cellrelay" hint="使用节次长度";

	property name="Term" fieldtype="many-to-one" fkcolumn="term_id" cfc="Term"  hint="学期";
	property name="ClassRoom" fieldtype="many-to-one" fkcolumn="cr_id" cfc="Classroom"  hint="教室";
	property name="Usage" fieldtype="many-to-one" fkcolumn="ut_id" cfc="ClassroomUseType"  hint="教室使用情况";

	/**
	* @hint "生成教室使用序号"
	* 
	* @Term "学期"
	* @ClassRoom "教室"
	*/
	public string function getUseIDX( required Term, required ClassRoom ) output=false {
		/* 
			教室使用序号规则 4位代码
		*/
		var nextID = numberFormat(1, "0___");
		
		var checkArray = ORMExecuteQuery("select UseIndex from ClassroomTimeTable 
											where 
											Term = :term and ClassRoom = :classroom
											order by UseIndex desc", {term=arguments.Term, classroom=arguments.ClassRoom}, { maxResults=1 });
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = numberFormat((checkArray[1] + 1), "0___");
		}

		return nextID;
		
	}
	
	/**
	* @hint "自定义主键规则"
	* 
	* @Term "学期"
	* @ClassRoom "教室"
	* @UseIndex "教室使用序号"
	*/
	public string function getID( required Term, required ClassRoom, required string UseIndex ) output=false {
		
		var nextID = arguments.Term.getTermID() & "," & arguments.ClassRoom.getClassroomID() & "," & arguments.UseIndex;

		/* 
			主键生成规则 
			
			教室使用编号21位 3位学期代码 , 12位教室编号 , 4位使用序号

			Unique: TERM_ID, CR_ID, UIDX
		*/
		return nextID;
	}
}

</cfscript>